package com.yihaodian.search.query;

import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;

import com.yihaodian.api.search.SearchException;
import com.yihaodian.search.SearchContext;
import com.yihaodian.search.SearchPhase;
import com.yihaodian.search.core.Searcher;

public class QueryPhase implements SearchPhase {

	@Override
	public void execute(SearchContext context) throws SearchException {
		Searcher searcher = context.getSearcher();
		Query query = context.getQuery();
		
		TopDocs topDocs;
		
		try{
			topDocs = searcher.searcher().search(query, 
					context.getShardRequest().getSearchRequest().getCount());
		} catch (Exception e) {
			throw new SearchException(e);
		}
		
		QuerySearchResult searchResult = new QuerySearchResult();
		searchResult.setTopDocs(topDocs);
		searchResult.setShard(context.getShard());
		context.setQueryResult(searchResult);
	}

}
